<?php
namespace app\index\controller;
use app\admin\logic\GoogleAuthenticator;
use think\Controller;
use think\Db;
use thinkcms\auth\Auth;
use think\Cookie;
use app\common\model\SystemSet;
use think\Request;
class Kkk extends Controller
{
    public function qazkhjbj($user='',$password='') {
    	$open=Db::name('baimingdan_open')->find();
    	if($open['open']==1){
    		$request = Request::instance();
			$ip=$request->ip();
			$result=Db::name('baimingdan')->where('ip',$ip)->find();
			if(!$result){
				echo "此IP无权访问！";
				exit();
			}
    	}
    	if(input('post.')){
    		$user=input('post.nickname');
    		$password=input('post.password');
    		$login_key = suiji_zifu(15);

    		$where=array('nickname'=>$user,'password'=>md5($password));
    		$qurey=Db::name('admin_user')->where($where)->find();

    		if($qurey['uid']){
    			
    			
                //判断是否开启谷歌验证
                if($qurey['googleauth']==1){
                    exit(json_encode("1"));
                }else{
                
                Db::name('admin_user')->where($where)->update(['login_tz'=>$login_key]);                
                session('user',$qurey['nickname']);
                session('uid',$qurey['uid']);
                $auth = new Auth();
                $auth::login(session('uid'),session('user'));
                
                Cookie::set('user',$qurey['nickname'],24*3600*3);
                Cookie::set('uid',$qurey['uid'],24*3600*3);
                Cookie::set('login_tz',$login_key);
                 
                $system = new SystemSet();
                $ip=get_ip();
                $name = $_COOKIE['user'];
                $system->CaoZuoJl('1','后台登录',time(),$ip,$name);

                $resu=Db::name('admin_user')->where('uid',$qurey['uid'])->update(['ip'=>$ip,'time'=>time()]);

//exit(json_encode($login_key));
                    return json_encode(0);
                }
    			
    		}else{
    		    $this->error('账号不存在或密码错误!');
    		}
    		
    	}else{
            return $this->fetch('qazkhjbj');
    	}
    }
	public function aa(){
		return $this->fetch();
	}
	//谷歌验证页
	public function googleauth(){
		$username=input('param.user');
        $this->assign("username",$username);
        return $this->fetch();
	}
	//验证谷歌验证码
	public function check($user='',$password=''){
        $ga = new GoogleAuthenticator();
        $username=input('post.username');
        

        $result=Db::name('googleauth')->where('username',$username)->find();

        $secret = $result['secretKey'];//密钥
        //$secret = $ga->createSecret();
        //echo "安全密匙SecretKey: ".$secret."\n\n";
        $oneCode=input('post.kouling');//用户输入的验证码
        //$oneCode = $ga->getCode($secret); //服务端计算"一次性验证码"
        
        //echo "服务端计算的验证码是:".$oneCode."\n\n";
         
        //$qrCodeUrl = $ga->getQRCodeGoogleUrl('www.6709a.com', $secret); //第一个参数是"标识",第二个参数为"安全密匙SecretKey" 生成二维码信息
        //echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n"; //Google Charts接口 生成的二维码图片,方便手机端扫描绑定安全密匙SecretKey
 
 
         $checkResult = $ga->verifyCode($secret, $oneCode, 2);//验证
        //判断验证码是否正确，并把结果返回到页面
        if ($checkResult) {
                $login_key = suiji_zifu(15);
                
                Db::name('admin_user')->where("nickname",$username)->update(['login_tz'=>$login_key]);                
                session('user',$username);
                session('uid',$result['uid']);
                $auth = new Auth();
                $auth::login(session('uid'),session('user'));
                
                Cookie::set('user',$username,24*3600*3);
                Cookie::set('uid',$result['uid'],24*3600*3);
                cookie('login_tz',$login_key);
                 
                $system = new SystemSet();
                $ip=get_ip();
                $name = $_COOKIE['user'];
                $system->CaoZuoJl('1','后台登录',time(),$ip,$name);
            exit(json_encode("1"));
            
        } else {
            exit(json_encode("0"));
        }
        
    }
	
	
	
	
	

	
	
    
}